package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> menuWithTree() {
        List<CategoryEntity> allCategoryList = list();
        return allCategoryList.stream().filter(c -> c.getParentCid() == 0L).peek(root->{
            root.setChildren(getChildren(root,allCategoryList));
        }).sorted(Comparator.comparingInt(CategoryEntity::getSort)).collect(Collectors.toList());
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //todo 删除之前判断菜单是否被引用
        removeByIds(asList);
    }

    @Override
    public Long[] findCateLogPath(Long catelogId) {
        List<Long> pathsList = new ArrayList<>();
        findParentPath(catelogId, pathsList);
        Collections.reverse(pathsList);
        return pathsList.toArray(new Long[0]);
    }

    @Override
    public void caseCadeUpdate(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updteCategoryAbout(category.getCatId(),category.getName());

    }

    private List<Long> findParentPath(Long cateLogId,List<Long> paths){
        paths.add(cateLogId);
        CategoryEntity byId = getById(cateLogId);
        if(byId.getParentCid() !=0){
            findParentPath(byId.getParentCid(), paths);
        }
        return paths;

    }
    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> allCategoryList) {
        return allCategoryList.stream().filter(curLvl -> curLvl.getParentCid().equals(root.getCatId()))
                .peek(curLvl -> {
                    // 找到当前菜单的子菜单
                    curLvl.setChildren(getChildren(curLvl, allCategoryList));
                }).sorted(Comparator.comparingInt(CategoryEntity::getSort)).collect(Collectors.toList());
    }
}